package com.yuwei.module_parking.view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.PopupMenu
import androidx.activity.viewModels
import androidx.appcompat.view.ContextThemeWrapper
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.bw.lib_base.path.RouterPath
import com.bw.lib_common.entity.AddParkingBody
import com.bw.lib_common.entity.AddParkingEntity
import com.bw.lib_common.entity.AddParkingItemEntity
import com.bw.lib_common.view.BaseMvvmActivity
import com.yuwei.module_parking.R
import com.yuwei.module_parking.adapter.AddParkingAdapter
import com.yuwei.module_parking.databinding.ActivityAddParkingBinding
import com.yuwei.module_parking.vm.ParkingViewModel

@Route(path = RouterPath.MODULE_ADD_PARKING_ACTIVITY)
class AddParkingActivity : BaseMvvmActivity<ActivityAddParkingBinding>() {
    private val vm: ParkingViewModel by viewModels()
    val addParkingZTAdapter = AddParkingAdapter()
    var lastSelectedPosition = -1
    var textZt:String = ""
    override fun initViews() {
        binding.parkingTt3.isEnabled = false
        binding.parkingTt2.isEnabled = false
        binding.addParkingZtRv.adapter = addParkingZTAdapter
        binding.addParkingZtRv.layoutManager = GridLayoutManager(this,3)
        addParkingZTAdapter.setOnItemClickListener { adapter, view, position ->
            lastSelectedPosition = position
            addParkingZTAdapter.setPosition(lastSelectedPosition)
            textZt = addParkingZTAdapter.data[lastSelectedPosition].title
            if (textZt == "已占用"){
                binding.parkingTt41.visibility = View.VISIBLE
                binding.parkingTt4.visibility =  View.VISIBLE
                binding.parkingTt51.visibility =  View.VISIBLE
                binding.parkingTt5.visibility =  View.VISIBLE
                binding.parkingTt51.visibility =  View.VISIBLE
                binding.parkingTt5.visibility =  View.VISIBLE
                binding.parkingTt61.visibility = View.VISIBLE
                binding.parkingTt6.visibility =  View.VISIBLE
            }else{
                binding.parkingTt41.visibility = View.GONE
                binding.parkingTt4.visibility =  View.GONE
                binding.parkingTt51.visibility =  View.GONE
                binding.parkingTt5.visibility =  View.GONE
                binding.parkingTt51.visibility =  View.GONE
                binding.parkingTt5.visibility =  View.GONE
                binding.parkingTt61.visibility = View.GONE
                binding.parkingTt6.visibility =  View.GONE
            }
            addParkingZTAdapter.notifyDataSetChanged()
        }
        binding.parkingEtIm1.setOnClickListener {
            val contextThemeWrapper = ContextThemeWrapper(this, com.bw.lib_common.R.style.CustomPopupMenu)
            val popupMenu = PopupMenu(contextThemeWrapper,it)
            popupMenu.menuInflater.inflate(R.menu.add_parking_menu,popupMenu.menu)
            popupMenu.setOnMenuItemClickListener { item ->
                binding.parkingTt2.setText(item.title)
                false
            }
            popupMenu.show()
        }


        binding.parkingEtIm2.setOnClickListener {
            val contextThemeWrapper = ContextThemeWrapper(this, com.bw.lib_common.R.style.CustomPopupMenu)
            val popupMenu = PopupMenu(contextThemeWrapper,it)
            popupMenu.menuInflater.inflate(R.menu.add_parking_cw_menu,popupMenu.menu)
            popupMenu.setOnMenuItemClickListener {item ->
                binding.parkingTt3.setText(item.title)
                false
            }
            popupMenu.show()
        }
        binding.parkingButton.setOnClickListener {
            if (binding.parkingTt1.text.toString().isEmpty()){
                ToastUtils.showShort("请输入车位编号")
                binding.parkingTt1.error = "请输入车位编号"
                return@setOnClickListener
            }
            if (binding.parkingTt2.text.toString().isEmpty()){
                ToastUtils.showShort("请选择停车位区域")
                binding.parkingTt2.error = "请选择停车位区域"
                return@setOnClickListener
            }
            if (binding.parkingTt3.text.toString().isEmpty()){
                ToastUtils.showShort("请选择停车位类型")
                binding.parkingTt3.error = "请选择停车位类型"
                return@setOnClickListener
            }
            if (addParkingZTAdapter.getPosition() == -1){
                ToastUtils.showShort("请选择车位状态")
                return@setOnClickListener
            }
            if (addParkingZTAdapter.getPosition() == 1){
                if (binding.parkingTt4.text.toString().isEmpty()){
                    ToastUtils.showShort("请输入使用人")
                    binding.parkingTt4.error = "请输入使用人"
                    return@setOnClickListener
                }
                val regex = Regex("^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][·]?[A-HJ-NP-Z0-9]{4,5}([A-HJ-NP-Z0-9挂学警港澳])?\$")
                if (!binding.parkingTt5.text.isEmpty()){
                    if (!regex.matches(binding.parkingTt5.text.toString())){
                        ToastUtils.showShort("车牌格式错误：请检查城市代码或新能源标识")
                        binding.parkingTt5.error = "车牌格式错误：请检查城市代码或新能源标识"
                        return@setOnClickListener
                    }
                }
                if (binding.parkingTt5.text.toString().isEmpty()){
                    ToastUtils.showShort("请输入车牌号码")
                    binding.parkingTt5.error = "请输入车牌号码"
                    return@setOnClickListener
                }
                if (binding.parkingTt6.text.toString().isEmpty()){
                    binding.parkingTt6.setText("无")
                }
            }

            val addParkingEntity = AddParkingBody(
                areaId = 2,
                areaName = binding.parkingTt2.text.toString(),
                id = 0,
                licensePlate = binding.parkingTt5.text.toString(),
                remarks = "",
                spaceNumber = binding.parkingTt1.text.toString(),
                spaceStatus = textZt,
                spaceType = binding.parkingTt3.text.toString(),
                userId = 11,
                userName = binding.parkingTt4.text.toString()
            )
            vm.addParkingList(addParkingEntity)
        }
        binding.addParkingBack.setOnClickListener {
            finish()
        }
        binding.parkingAdd.setOnClickListener {

        }
    }

    override fun loadData() {
        addParkingZTAdapter.data.clear()
        addParkingZTAdapter.data.add(AddParkingItemEntity("空闲",false))
        addParkingZTAdapter.data.add(AddParkingItemEntity("已占用",false))
        addParkingZTAdapter.data.add(AddParkingItemEntity("维护中",false))
        addParkingZTAdapter.notifyDataSetChanged()
        vm.getAddParkingSuccess.observe(this){
            ToastUtils.showLong("保存成功")
            finish()
        }

    }
    override fun observeDataChange() {

    }

    override fun getLayoutId(): Int = R.layout.activity_add_parking

}